package com.wsoft.system.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wsoft.constant.RedisKeyConstant;
import com.wsoft.core.annotation.GetRedisCache;
import com.wsoft.core.annotation.InitRedisCache;
import com.wsoft.core.service.RedisService;
import com.wsoft.core.utils.SqlAuthDisableHolder;
import com.wsoft.system.dto.SysDataPermDto;
import com.wsoft.system.entity.SysDataPermEntity;
import com.wsoft.system.entity.SysMemberOrgRoleRelationEntity;
import com.wsoft.system.entity.SysRolePermRelationEntity;
import com.wsoft.system.mapper.SysDataPermMapper;
import com.wsoft.system.mapper.SysMemberOrgRoleRelationMapper;
import com.wsoft.system.mapper.SysRolePermRelationMapper;
import com.wsoft.system.query.perm.SysDataPermReq;
import com.wsoft.system.service.ISysDataPermService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 数据权限表 服务实现类
 * </p>
 *
 * @author kyle
 * @since 2024年08月29日 10:34:35
 */
@Service
public class SysDataPermServiceImpl extends ServiceImpl<SysDataPermMapper, SysDataPermEntity> implements ISysDataPermService {

    @Resource
    private SysRolePermRelationMapper rolePermRelationMapper;
    @Resource
    private SysMemberOrgRoleRelationMapper memberOrgRoleRelationMapper;
    @Resource
    private RedisService redisService;
    @Override
    public boolean insert(SysDataPermDto dto) {
        SysDataPermEntity entity = Convert.convert(SysDataPermEntity.class, dto);
        return save(entity);
    }

    @Override
    @InitRedisCache(types = {RedisKeyConstant.DATA_PERM ,RedisKeyConstant.DEFAULT_DATA_PERM},
            values ={"id" , RedisKeyConstant.REDIS_NULL} )
    public boolean update(SysDataPermDto dto) {
        SysDataPermEntity entity = Convert.convert(SysDataPermEntity.class, dto);
        QueryWrapper<SysRolePermRelationEntity> query = new QueryWrapper<>();
        query.lambda().eq(SysRolePermRelationEntity::getPermId,dto.getId());
        List<SysRolePermRelationEntity> entities = rolePermRelationMapper.selectList(query);
        entities.forEach(e -> {
            redisService.del(RedisKeyConstant.ROLE_DATA_PERM_CACHE + e.getRoleId());
            QueryWrapper<SysMemberOrgRoleRelationEntity> roleQuery = new QueryWrapper<>();
            roleQuery.lambda().eq(SysMemberOrgRoleRelationEntity::getRoleId,e.getRoleId());
            List<SysMemberOrgRoleRelationEntity> roleEntities = memberOrgRoleRelationMapper.selectList(roleQuery);
            roleEntities.forEach(roleEntity -> {
                redisService.del(RedisKeyConstant.MEMBER_ROLE_CACHE + roleEntity.getMemberId());
            });
        });
        return updateById(entity);
    }

    @Override
    @InitRedisCache(types = {RedisKeyConstant.DATA_PERM ,RedisKeyConstant.DEFAULT_DATA_PERM},
            values ={RedisKeyConstant.REDIS_SELF , RedisKeyConstant.REDIS_NULL} )
    public boolean delete(Long id) {
        QueryWrapper<SysRolePermRelationEntity> query = new QueryWrapper<>();
        query.lambda().eq(SysRolePermRelationEntity::getPermId,id);
        List<SysRolePermRelationEntity> entities = rolePermRelationMapper.selectList(query);
        entities.forEach(e -> {
            redisService.del(RedisKeyConstant.ROLE_DATA_PERM_CACHE + e.getRoleId());
            QueryWrapper<SysMemberOrgRoleRelationEntity> roleQuery = new QueryWrapper<>();
            roleQuery.lambda().eq(SysMemberOrgRoleRelationEntity::getRoleId,e.getRoleId());
            List<SysMemberOrgRoleRelationEntity> roleEntities = memberOrgRoleRelationMapper.selectList(roleQuery);
            roleEntities.forEach(roleEntity -> {
                redisService.del(RedisKeyConstant.MEMBER_ROLE_CACHE + roleEntity.getMemberId());
            });
        });
        return removeById(id);
    }

    @Override
    @GetRedisCache(type = RedisKeyConstant.DATA_PERM,
                    value = "id")
    public SysDataPermEntity getDataPerm(Long id) {
        return getById(id);
    }

    @Override
    public PageInfo<SysDataPermEntity> pageDataPerm(SysDataPermReq req) {
        PageHelper.startPage(req.getPageNum(), req.getPageSize());
        QueryWrapper<SysDataPermEntity> query = new QueryWrapper<>();
        //名称
        if (StrUtil.isNotBlank(req.getDataPermName())) {
            query.lambda().like(SysDataPermEntity::getDataPermName, req.getDataPermName());
        }
        //数据权限规则类型
        if (req.getRuleType() != null) {
            query.lambda().eq(SysDataPermEntity::getRuleType, req.getRuleType());
        }
        //表单名称
        if (StrUtil.isNotBlank(req.getTableName())) {
            query.lambda().like(SysDataPermEntity::getTableName, req.getTableName());
        }
        return PageInfo.of(list(query));
    }

    @Override
    @GetRedisCache(type = RedisKeyConstant.DEFAULT_DATA_PERM,
            value =RedisKeyConstant.REDIS_NULL )
    public List<SysDataPermEntity> getSpecificDataPerm() {
        SqlAuthDisableHolder.open();
        QueryWrapper<SysDataPermEntity> query = new QueryWrapper<>();
        query.lambda().isNull(SysDataPermEntity::getTableName);
        List<SysDataPermEntity> list = list(query);
        SqlAuthDisableHolder.close();
        return list;
    }
}
